/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.cell;

import java.util.HashMap;
import java.util.Map;

import com.reportgear.report.core.cell.attr.CellExpandAttr;
import com.reportgear.report.view.grid.editor.CellEditor;
import com.reportgear.report.view.grid.editor.CellEditorManager;

/**
 * 报表设计时单元格基类
 * 
 * @version 1.0 2011-3-9
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class DefaultCell extends AbstractCell {
	// 扩展属性名
	private static final String PROPERTY_CELL_EXPAND = "cell.expand.attr";

	// 属性
	private Map<String, Object> attributes;
	// 单元格编辑器
	private CellEditorManager cellEditorMgr = CellEditorManager.INSTANCE;

	/**
	 * 构造函数
	 * 
	 * @param column
	 *            列
	 * @param row
	 *            行
	 */
	public DefaultCell(int column, int row) {
		super(column, row);
	}

	/**
	 * 获取单元格编辑器
	 * 
	 * @return 单元格编辑器
	 */
	public CellEditor getCellEditor() {
		return cellEditorMgr.getCellEditor(this.getValue());
	}

	/**
	 * 获取单元格属性
	 * 
	 * @return 属性映射表
	 */
	private Map<String, Object> getAttributes() {
		if (this.attributes == null) {
			this.attributes = new HashMap<String, Object>();
		}

		return this.attributes;
	}

	/**
	 * 设置单元格扩展属性
	 * 
	 * @param cellExpandAttr
	 *            扩展属性
	 */
	public void setCellExpandAttr(CellExpandAttr cellExpandAttr) {
		if (cellExpandAttr == null) {
			this.getAttributes().remove(PROPERTY_CELL_EXPAND);
		} else {
			this.getAttributes().put(PROPERTY_CELL_EXPAND, cellExpandAttr);
		}
	}

	/**
	 * 获取扩展属性
	 * 
	 * @return 扩展属性
	 */
	public CellExpandAttr getCellExpandAttr() {
		Object o = this.getAttributes().get(PROPERTY_CELL_EXPAND);

		if (o == null) {
			return CellExpandAttr.DEF_CELL_EXPAND_ATTR;
		}

		return (CellExpandAttr) o;
	}

	@Override
	public DefaultCell clone() {
		DefaultCell cell = new DefaultCell(this.getColumn(), this.getRow());

		cell.attributes = this.attributes;
		cell.setColumnSpan(this.getColumnSpan());
		cell.setRowSpan(this.getRowSpan());
		cell.setMerged(this.isMerged());
		cell.setValue(this.getValue());
		cell.setStyle(this.getStyle());

		if (cell.isMerged()) {
			CR cr = this.getMergedToCR();
			cell.mergeIntoCell(cr.getColumn(), cr.getRow());
		}

		return cell;
	}
}
